package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Feedback;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.FeedbackDto;
import com.wzcl.app.model.vo.FeedbackVo;
import com.wzcl.app.service.FeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/feedback")
@Api(value = "用户反馈controller", tags = {"用户反馈接口"})
public class FeedbackController {

    @Autowired
    private FeedbackService service;

    @ApiOperation("添加反馈")
    @PostMapping("/add")
    public RVO<FeedbackVo> addFeedback(@RequestBody @Valid FeedbackDto feedbackDto){
        feedbackDto.setProvider(SecurityContextHolder.getContext().getAuthentication().getName());
        Feedback feedback = service.addFeedback(feedbackDto);
        return RVO.success(FeedbackVo.create(feedback));
    }

    @ApiOperation("处理反馈信息")
    @PostMapping("/process")
    public RVO<FeedbackVo> processFeedback(@RequestBody FeedbackDto feedbackDto){
        feedbackDto.setProcessor(SecurityContextHolder.getContext().getAuthentication().getName());
        Feedback feedback = service.processFeedback(feedbackDto);
        return RVO.success(FeedbackVo.create(feedback));
    }

    @ApiOperation("评价反馈信息")
    @PostMapping("/comment")
    public RVO<FeedbackVo> commentFeedback(@RequestBody FeedbackDto feedbackDto){
        Feedback feedback = service.commentFeedback(feedbackDto);
        return RVO.success(FeedbackVo.create(feedback));
    }

    @ApiOperation("查看反馈List")
    @PostMapping("/getList")
    public RVO<List<FeedbackVo>> getDeliveryList(@RequestBody FeedbackDto feedbackDto){
        List<Feedback> list = service.getFeedbackList(feedbackDto);
        if(list != null && list.size()>0){
            return RVO.success(list.stream().map(FeedbackVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @ApiOperation("查看反馈Page")
    @PostMapping("/getPage")
    public RVO<List<FeedbackVo>> getDeliveryPage(@RequestBody FeedbackDto feedbackDto){
        MyPage<Feedback> page = service.getFeedbackPage(feedbackDto);
        if(page != null && page.getContent().size()>0){
            return RVO.success(page.getContent().stream().map(FeedbackVo::create).collect(Collectors.toList()), page);
        }
        return RVO.success(null);
    }
}
